//
//  GetCityLocationViewController.m
//
//
//  Created by gynjobs on 2017/8/14.
//  Copyright © 2017年 gynjobs. All rights reserved.
//

#import "GetCityLocationViewController.h"
#import "CityInfo.h"
@interface GetCityLocationViewController ()<UITableViewDelegate,UITableViewDataSource,CityLocationDelegate,SearchBarDelegate>
@property (nonatomic,strong) NSMutableArray *testArr;

@end

@implementation GetCityLocationViewController

-(instancetype)init{
    
    if(self = [super init]){
        
        
        self.cityAction = [[CityLocationAction alloc] init];
        self.testArr = [NSMutableArray array];
    }
    
    return self;
    
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}


-(void)loadView{
    [super loadView];
    
    self.mainView = [[CityLocationView alloc] init];
    self.view = self.mainView;
    
}

-(void)viewDidLoad{
    [super viewDidLoad];
    self.title = @"选择城市";
    
    self.mainView.cityTableView.dataSource = self;
    self.mainView.cityTableView.delegate = self;
    self.mainView.cityDelegate = self;
    self.mainView.searchBarView.searchDelegate = self;
    
    NSArray * chots = [CityInfo getCacheHotCityList];
    if(chots && [chots count] > 0){
        //        mainView.cityTableView.tableHeaderView = [mainView genTableViewHeaderView:[cityAction hotArray]];
        self.mainView.cityTableView.tableHeaderView = [self.mainView genTableViewHeaderView:chots];
    }
    
    [self.cityAction start:^{
        [self refreshCityTableView];
        [self.mainView setCallBackData:[self.cityAction hotArray] withDataSource:[self.cityAction dataArray]];
        
    }];
    
}


#pragma mark -
#pragma mark tableview datasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [[self.cityAction dataArray] count ];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    id dic   = [[self.cityAction dataArray] objectAtIndex:section];
    NSArray * subArray = [dic valueForKey:@"subs"];
    return [subArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * cellId = @"city.cell.id";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    CityInfo * city = [self.cityAction getCityItem:indexPath];
    if(!cell){
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    cell.textLabel.text = city.zone_name;
    return cell;
}


- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    NSMutableArray *array = [self.cityAction letterArray];
    NSMutableArray * farray = [NSMutableArray arrayWithObjects:@"热", nil];
    [farray addObjectsFromArray:array];
    return farray;
}


- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    NSMutableArray *array = [self.cityAction letterArray];
    if([array count] == 0){
        return nil;
    }
    return [array objectAtIndex:section];
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
    if (index == 0) {
        [tableView setContentOffset:CGPointMake(0, 0) animated:YES];
        return NSNotFound;
    }
    return index - 1;
}



#pragma mark -
#pragma mark tableview delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    CityInfo * city = [self.cityAction getCityItem:indexPath];
    [self callBackWithCityInfo:city];
}



-(void)refreshCityTableView{
    
    [self.mainView.cityTableView reloadData];
}


#pragma mark -
#pragma mark city location delegate

-(void)cityLocationHotIndex:(NSInteger)index{
    
    if(index < 0 || index > [[self.cityAction hotArray] count] -1){
        
        return;
    }
    
    CityInfo * city = [[self.cityAction hotArray] objectAtIndex:index];
    [self callBackWithCityInfo:city];
    
}

-(void)callBackWithCityInfo:(CityInfo*)city{
    
    
    if(self.itemData){
        
        NSMutableDictionary * dic = [NSMutableDictionary dictionaryWithCapacity:2];
        [dic setValue:city.zone_name forKey:@"data"];
        [dic setValue:city.zone_id forKey:@"id"];
        [dic setValue:city.s_EN forKey:@"s_EN"];
        [dic setValue:city.CN forKey:@"CN"];
        [self callBackDelegate:dic];
        
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark search bar delegate

-(void)citySearchBar:(id)data{
    if(!data){
        return;
    }
    
    [self callBackWithCityInfo:data];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
